<?php
/**
 *
 * @author Dima
 *
 */
class Lemar_File
{
	
//----------------------------------------------------------------------------------------------------------------
public static function checkAttachments($sName,  $sValue)
{
	$oWedding = Lemar_Wedding::getCurrentWedding();
	$iIdWedding = $oWedding->getId();

	$oVendor = models_VendorMapper::findByIdWeddingAndColumn($iIdWedding, 'id', (int)$sValue);

	$iSize =  sizeof($oVendor); 
	if ($iSize > 0)
	{
		return true;
	}
	else
	{
		return false;
	} 
}


//----------------------------------------------------------------------------------------------------------------
/*
 *  $sName - 
 *  $sValue - id images
 *  
 */
public static function checkImage($sName,  $sValue)
{
	$oWedding = Lemar_Wedding::getCurrentWedding();
	$iIdWedding = $oWedding->getId();

	$oVendor = models_FolderFileMapper::findByIdWeddingAndId($iIdWedding, $sValue);
	
	$iSize =  sizeof($oVendor); 
	if ($iSize > 0)
	{
		return true;
	}
	else
	{
		return false;
	} 
}


//----------------------------------------------------------------------------------------------------------------
/*
 *  $sName - 
 *  $sValue - id folder
 *  
 */
public static function checkLogo()
{
	$oWedding = Lemar_Wedding::getCurrentWedding();
	$iIdWedding = $oWedding->getId();

	$oVendor = Lemar_Thumbnail::getImageLogoPath($iIdWedding);
	$iSize =  strlen($oVendor);
	
	if ($iSize > 0)
	{
		$handle = fopen('.' . $oVendor, "r");
		echo $contents = fread($handle, filesize('.' . $oVendor));
		fclose($handle);
	}
	return false;
}


//----------------------------------------------------------------------------------------------------------------
/*
 *  $sName - 
 *  $sValue - id folder
 *  
 */
public static function checkFolder($sName,  $sValue)
{
	$oWedding = Lemar_Wedding::getCurrentWedding();
	$iIdWedding = $oWedding->getId();

	$oVendor = models_FolderMapper::findByIdAndIdWedding($sValue, $iIdWedding);
	
	$iSize =  sizeof($oVendor); 
	if ($iSize > 0)
	{
		return true;
	}
	else
	{
		return false;
	} 
}


//----------------------------------------------------------------------------------------------------------------
public static function sendFile($response, $path, $type, $options = array())
{
    if (!is_readable($path) || !$response->canSendHeaders()) 
    {
    	return false;
	}
 
    // Set the cache-control
    if (isset($options['cache'])) 
    {
    	$this->setCacheHeaders($options['cache']);
	}
 
    // Get the last modified time
    if (isset($options['modified'])) 
    {
    	$modified = (int) $options['modified'];
	} 
	else 
	{
    	$modified = filemtime($path);
	}

	// Set the file name
    if (isset($options['filename']) && !empty($options['filename'])) 
    {
    	$filename = $options['filename'];
	} 
	else 
	{
    	$filename = basename($path);
	}
 
	// Set the content disposition
    if (isset($options['disposition']) && $options['disposition'] == 'inline') 
    {
    	$disposition = 'inline';
	} 
	else 
	{
    	$disposition = 'attachment';
	}
 
    $response->setHttpResponseCode(200);
    $response->setHeader('Content-Type', $type, true);
    $response->setHeader('Content-Disposition', $disposition . '; filename="' . $filename . '"', true);
 
    // Do we want to use the X-Sendfile header or stream the file
    if (isset($options['xsendfile']) && $options['xsendfile']) 
    {
		$response->setHeader('X-Sendfile', $path);
    	$response->sendHeaders();
    	return true;
    }
 
	$response->setHeader('Last-Modified', gmdate('r', $modified), true);
	$response->setHeader('Content-Length', filesize($path), true);
	$response->sendHeaders();
 
	readfile($path);
	return true;
}

}	//class